package com.exception;

import com.component.ErrorCode;
import com.response.BaseResponse;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @ClassName ExceptionCatch
 * @Description 异常捕获 异常格式显示
 * @Author XiangQian
 * @Date 2022/7/7 16:11
 */
public class ExceptionCatch {


    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);

    /**
     * ImmutableMap
     * 创建一个对象常量映射，来保存一些常量映射的键值对
     **/
    private static ImmutableMap<Class<? extends Throwable>, ErrorCode> EXCEPTIONS;

    /**
     * @Description 调用UserException封装
     */
    @ExceptionHandler(UserException.class)
    @ResponseBody
    public BaseResponse customException(UserException e) {
        LOGGER.error("Error message is {}", e.getMessage());
        return new BaseResponse(e.getErrorCode());
    }


}
